/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Ercode;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.ErcodeService;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * @author fupan on 2021/10/16.
 */
@RestController
@RequestMapping("/admin/ercode")
public class ErcodeController {

    @Autowired
    private ErcodeService ercodeService;

    @Autowired
    private ProductService productService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin:ercode:page')")
    public ResponseEntity<IPage<Ercode>> page(Ercode ercode, PageParam<Ercode> page) {
        IPage<Ercode> ercodeIPage = ercodeService.page(page,
                new LambdaQueryWrapper<Ercode>()
                        .eq(ercode.getStatus() != null, Ercode::getStatus, ercode.getStatus())
                        .orderByDesc(Ercode::getSeq));
        return ResponseEntity.ok(ercodeIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{imgId}")
    @PreAuthorize("@pms.hasPermission('admin:ercode:info')")
    public ResponseEntity<Ercode> info(@PathVariable("imgId") Long imgId) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        Ercode ercode = ercodeService.getOne(new LambdaQueryWrapper<Ercode>().eq(Ercode::getShopId, shopId).eq(Ercode::getImgId, imgId));
        if (Objects.nonNull(ercode.getRelation())) {
            Product product = productService.getProductByProdId(ercode.getRelation());
            ercode.setPic(product.getPic());
            ercode.setProdName(product.getProdName());
        }
        return ResponseEntity.ok(ercode);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin:ercode:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid Ercode ercode) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        ercode.setShopId(shopId);
        ercode.setUploadTime(new Date());
        ercodeService.save(ercode);
        ercodeService.removeErcodeCache();
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:ercode:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid Ercode ercode) {
        ercodeService.updateById(ercode);
        ercodeService.removeErcodeCache();
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:ercode:delete')")
    public ResponseEntity<Void> delete(@RequestBody Long[] ids) {
        ercodeService.deleteErcodeByIds(ids);
        ercodeService.removeErcodeCache();
        return ResponseEntity.ok().build();
    }
}
